package lixp.redis.controller;

import com.github.pagehelper.PageInfo;
import lixp.pojo.CmsArticle;
import lixp.redis.service.RedisService;
import lixp.res.Result;
import lixp.res.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class RedisController {

    @Autowired
    RedisService redisService;

    @RequestMapping("/findAll")
    public Result findAll(Integer pageNum){
        PageInfo pageInfo = redisService.findAll(pageNum);
        return ResultUtil.success(pageInfo);
    }

    @RequestMapping("/findById/{id}")
    public Result findById(@PathVariable("id")Integer id){

        CmsArticle cmsArticle= redisService.getCmsArticleById(id);
        return ResultUtil.success(cmsArticle);
    }

    @PostMapping("/add")
    public Result add(CmsArticle cmsArticle)  {
        System.out.println(cmsArticle);
        boolean flag = redisService.add(cmsArticle);
        if (flag){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(0,"保存失败");
        }
    }

    @PostMapping("/update")
    public Result update(CmsArticle cmsArticle)  {
        System.out.println(cmsArticle);
        CmsArticle cmsArticleRes = redisService.update(cmsArticle);
        if (cmsArticleRes != null){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(0,"保存失败");
        }
    }

    @RequestMapping("/del/{id}")
    public Result delById(@PathVariable("id") Integer id){
        boolean flag = redisService.delById(id);
        if (flag){
            return ResultUtil.success();
        }else return ResultUtil.error(0,"失败");
    }
}
